package cn.buk.qms.task;

import cn.buk.qms.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 该定时任务主要是将tms中的数据定时保存到eotms的redis中, 供eotms使用
 *
 * @author yfdai
 */
@Profile({"prod"})
@Component
public class FifthTask {


  private static final int SERVER_FIFTH = 5;

  /**
   * 当前应用所在服务器的id
   */
  private final int serverId;

  private final EnterpriseService enterpriseService;

  @Autowired
  public FifthTask(@Value("${server_id}") int serverId,
                   EnterpriseService enterpriseService) {
    this.serverId = serverId;
    this.enterpriseService = enterpriseService;
  }

  @Async
  @Scheduled(cron = "0 0/10 * * * *")
  public void saveBasicData2Redis() {
    if (serverId == SERVER_FIFTH) {
      //把静态数据放到redis中方便微服务调用
      enterpriseService.saveBasicData2Redis();
    }
  }

  @Async
  @Scheduled(cron = "0 0/5 * * * *")
  public void saveBusinessData2Redis() {
    if (serverId == SERVER_FIFTH) {
      //把静态数据放到redis中方便微服务调用
      enterpriseService.saveBusinessData2Redis();
    }
  }

}
